<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 商家自动接单
 */

namespace app\api\controller\common\v1_0;

class Rider extends Common
{

    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 自动接单/骑手
     */    
    public function index(){
      
        // $rider = (new \app\common\model\Rider())->alias('rider')
        // ->field([
        //     'count(rider_id) as count',
        //     'rider.id','rider.receiving_num',
        //     'rider.longitude','rider.latitude'
        //     ])
        // ->join("{$this->tdb}orders orders","orders.rider_id = rider.id")
        // ->where('orders.rider_success_time','>',time()-86400)
        // ->where([
        //     'rider.status' => 1,'rider.is_state' => 1
        // ])
        // ->order('count asc ,distance desc')
        // ->group('rider_id')
        // ->select();

        $rider = [
            0=>['id' => 1 , 'receiving_num' => 3 ,'longitude' => '125.37398','latitude' => '44.02842' ,'store_latitude' => '43.17186' ,'store_longitude' => '124.35161' ,'title' => '长春到四平:'],
            1=>['id' => 2 , 'receiving_num' => 3 ,'longitude' => '125.37398','latitude' => '44.02842' ,'store_latitude' => '45.15239' ,'store_longitude' => '124.81596','title' => '长春到松原:'],
            2=>['id' => 3 , 'receiving_num' => 3 ,'longitude' => '125.37398','latitude' => '44.02842' ,'store_latitude' => '43.74598' ,'store_longitude' => '125.90301','title' => '长春到万:'],
        ];
        $order = [
            0=>['store_latitude'=>'43.83245','store_longitude'=>'126.55726','title' => '长春到吉林:'] 
        ];
        
        foreach ($rider as $key => $value) {
            #计算在线订单数量
            // $order_count = (new \app\common\model\Orders())
            // ->where(['rider_id' => $value['id']])
            // ->whereIn('rider_status',[1,2,3])
            // ->where('rider_success_time','>',time()-86400)
            // ->count();
            #按单量
            // if($value['receiving_num'] < $order_count || $order_count == 0){
            //     (new \app\common\model\Orders())
            //     ->where(['type' => 1,'pay_status' => 1, 'rider_id' => 0,'rider_turn_status' => 0])
            //     ->update([
            //         'rider_status' => 1,
            //         'rider_id' => $value['id'],
            //         'rider_receiving_time' => time(),
            //     ]);
            // }

            #骑手有单子去往的方向
            // $rider_order = (new \app\common\model\Orders())->whereIn('rider_status',[1,2,3])->order('id asc')->find();
            #接单去商家的方向
            // if($rider_order['rider_status'] == 1){
                $cc=$this->getDirection($value['latitude'],$value['longitude'],$value['store_latitude'],$value['store_longitude']);
                // $order =  (new \app\common\model\Orders())->where(['type' => 1,'pay_status' => 1, 'rider_id' => 0,'rider_turn_status' => 0])->select();
                foreach ($order as $key => $val) {
                    $bb=$this->getDirection($value['latitude'],$value['longitude'],$val['store_latitude'],$val['store_longitude']);
                    if($bb == $cc){
                        echo $val['title'].$value['id']."可以接单";
                    }
                    echo($val['title']."$bb");
                    echo "<br>";
                }
                echo($value['id'].$value['title'].$cc);
                echo "<br>";
                echo "<br>";
            // }
           
         
        }
        // return self::successResult("成功");
    }

    //根据经纬度计算方向 longitude经度 

    public function getDirection($lat1, $lng1, $lat2, $lng2){
        $k1 = $lng2-$lng1;
        $k2 = $lat2-$lat1;
        if( 0 == $k1){
            if($k2>0){
                $str="北";
            }else if( $k2<0){
                $str ="南";
            }else if( $k2 == 0){
                $str="附近";
            }
        }else if( 0 == $k2){
                if($k1>0){
                $str="东";
            }else if( $k1<0){
                $str="西";
            }
        }else{
            $k=$k2/$k1;
            if($k2>0){
                if($k1>0){
                    $angle = 180*atan($k)/M_PI;
                    $str="东偏北";
                }else if($k1<0){
                    $angle = 180*atan(-$k)/M_PI;
                    $str= "西偏北";
                }
            }else if($k2<0){
                if($k1<0){
                    $angle = 180*atan($k)/M_PI;
                    $str = "西偏南";
        }else if($k1>0){
            $angle = 180*atan($k)/M_PI;
            $str="东偏南";
        }
    }
}
        return $str;
    }

}
